Skip to content

Apply value-assertion rules consistently to Should-* siblings#2707

Merged
nohwnd merged 1 commit into
pester:mainfrom
nohwnd:fix-should-assertion-consistency
Jun 11, 2026
Merged

Apply value-assertion rules consistently to Should-* siblings#2707
nohwnd merged 1 commit into
pester:mainfrom
nohwnd:fix-should-assertion-consistency

Conversation

@nohwnd

@nohwnd nohwnd commented Jun 11, 2026

Copy link
Copy Markdown
Member

The Pester 6 assertion rules in docs/assertion-types.md say that generic value assertions reject collections on -Expected, and that value assertions unwrap pipeline input via Collect-Input -UnrollInput. A handful of Should-* didn't follow either rule and behaved differently from their siblings.

Caught by auditing all Should-* for Collect-Input / Ensure-ExpectedIsNotCollection usage and reproducing the gaps with a built module:

  • Should-NotBeString — missing Collect-Input. Multi-item pipeline silently used the last item; non-string -Actual silently passed the assertion (Test-StringEqual returned $false, no throw). Now mirrors Should-NotBeLikeString and throws ArgumentException when -Actual isn't a string.
  • Should-BeAfter / Should-BeBefore — missing Collect-Input. Also missing the -Because parameter declaration entirely, so the documented switch was a no-op (the throw path read $Because from an unbound variable). Both fixed.
  • Should-BeSame / Should-NotBeSame — missing Ensure-ExpectedIsNotCollection, so passing @() to -Expected produced a misleading "not the same instance" failure instead of the standard collection-on--Expected ArgumentException.

Tests added next to each fix lock in the rule.

All 653 tests under tst/functions/assert pass on net8.0.

The Pester 6 assertion rules in docs/assertion-types.md say generic
value assertions reject collections on -Expected, and value
assertions unwrap pipeline input via Collect-Input -UnrollInput.
A handful of Should-* didn't follow either rule, behaving differently
from their siblings:

- Should-NotBeString: missing Collect-Input. Multi-item pipeline
  silently used the last item; non-string Actual silently passed
  the assertion. Now mirrors Should-NotBeLikeString and throws
  ArgumentException when Actual isn't a string.
- Should-BeAfter / Should-BeBefore: missing Collect-Input. Also
  missing the -Because parameter declaration entirely, so the
  documented switch was a no-op. Both fixed.
- Should-BeSame / Should-NotBeSame: missing Ensure-ExpectedIsNotCollection,
  so passing @() to -Expected produced a misleading "not the same
  instance" failure instead of the standard collection-on-Expected
  error.

Tests added to lock in each rule.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd enabled auto-merge (squash) June 11, 2026 16:05
@nohwnd nohwnd merged commit e70b372 into pester:main Jun 11, 2026
13 checks passed
nohwnd added a commit that referenced this pull request Jun 11, 2026
The doc example `(Get-Date).AddDays(1) | Should-BeAfter (Get-Date)`
errored with "Cannot bind positional parameters because no names were
given." The `Fluent` and `Expected` parameter sets both declared a
position-0 parameter (`$Time` and `$Expected`), so a `DateTime` at
position 0 matched both sets and PowerShell could not pick one.

Split the `Fluent` parameter set into `FluentAgo` and `FluentFromNow`,
each requiring its mandatory switch. With `-Ago`/`-FromNow` no longer
optional within their set, neither fluent set is satisfiable from just a
positional `[DateTime]`, so the `Expected` set wins unambiguously.

Side effects:
- `Should-BeAfter 10minutes -Ago -FromNow` now fails at binding (no
  parameter set can be resolved) instead of the runtime check.
- The runtime "you must provide -Ago or -FromNow" check is gone, since
  binding enforces it.

Added doc-style positional tests on both `Should-BeAfter` and
`Should-BeBefore`. Verified the full Pester suite: 2171 passed, 0
failed.

Follow-up to #2707.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant